package calendar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class MainMenu {
	public static void main(String[] args){
		CalendarUtil cu = new CalendarUtil();
		ShowUtil su = new ShowUtil();
		Scanner input = new Scanner(System.in);
		while(true){
			su.showMainMenu();
			int order = getIntFromInput(input);
			if(order == -1){
				continue;
			}
			if(order == 3){
				System.exit(0);
			}else if(order == 1){
				doShowCalendar(cu, su, input);
			}else if(order == 2){
				doMarkCalendar(cu, su, input);
			}else{	
				System.out.println("Please input 1, 2, or 3");
				continue;
			}
		}
	}
	
	public static void doShowCalendar(CalendarUtil cu, ShowUtil su, Scanner input){
		while(true){
			System.out.println();
			su.showShowMenu();
			int order = getIntFromInput(input);
			if(order == -1){
				continue;
			}else if(order == 1){
				System.out.print("Month: ");
				int month = getIntFromInput(input);
				if(month > 12 || month < 1){
					System.out.println("Please input a number between 1 to 12");
					continue;
				}
				su.showMonth(month, cu);
			}else if(order == 2){
				System.out.print("Date: ");
				int month = getIntFromInput(input);
				if(month == -1){
					continue;
				}
				int day = getIntFromInput(input);
				if(day == -1){
					continue;
				}
				su.showAppointmentByDate(month, day, cu);
			}else if(order == 3){
				System.out.print("Month: ");
				int month = getIntFromInput(input);
				if(month == -1){
					continue;
				}
				su.showAppointmentByMonth(month, cu);
			}else if(order == 4){
				System.out.print("Month: ");
				int month = getIntFromInput(input);
				if(month > 12 || month < 1){
					System.out.println("Please input a number between 1 to 12");
					continue;
				}
				su.showHolidaysByMonth(month, cu);
			}else if(order == 5){
				return;
			}else{
				System.out.println("Please input a number between 1 to 5");
			}
		}
	}
	
	public static void doMarkCalendar(CalendarUtil cu, ShowUtil su, Scanner input){
		List<String> weekList = new ArrayList<String>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
		while(true){
			System.out.println();
			su.showMarkMenu();
			int order = getIntFromInput(input);
			if(order == -1){
				continue;
			}else if(order == 1){
				System.out.print("Date: ");
				int month = getIntFromInput(input);
				if(month > 12 || month < 1){
					continue;
				}
				int day = getIntFromInput(input);
				if(day > cu.getDayOfMonth(month) || day < 1){
					continue;
				}
				System.out.print("Event: ");
				String event = getStringFromInput(input);
				cu.addAppointment(month, day, event);
			}else if(order == 2){
				System.out.print("Day: ");
				String week = getStringFromInput(input);
				if(! weekList.contains(week)){
					System.out.println("Please input day as Mon, Tus, Wed...");
					continue;
				}
				int weekNum = 0;
				switch(week){
				case "Sun" : weekNum = 0;break;
				case "Mon" : weekNum = 1;break;
				case "Tue" : weekNum = 2;break;
				case "Wed" : weekNum = 3;break;
				case "Thu" : weekNum = 4;break;
				case "Fri" : weekNum = 5;break;
				case "Sat" : weekNum = 6;break;
				}
				System.out.print("Event: ");
				String event = getStringFromInput(input);
				System.out.println("From: ");
				int startMonth = getIntFromInput(input);
				if(startMonth > 12 || startMonth < 1){
					continue;
				}
				int startDay = getIntFromInput(input);
				if(startDay > cu.getDayOfMonth(startMonth) || startDay < 1){
					continue;
				}
				System.out.println("To:	");
				int endMonth = getIntFromInput(input);
				if(endMonth > 12 || endMonth < 1){
					continue;
				}
				int endDay = getIntFromInput(input);
				if(endDay > cu.getDayOfMonth(endMonth) || endDay < 1){
					continue;
				}
				cu.addWeeklyAppointment(startMonth, startDay, endMonth, endDay, weekNum, event);
			}else if(order == 3){
				System.out.print("Date: ");
				int month = getIntFromInput(input);
				if(month > 12 || month < 1){
					continue;
				}
				int day = getIntFromInput(input);
				if(day > cu.getDayOfMonth(month) || day < 1){
					continue;
				}
				cu.deleteAppoint(month, day);
			}else if(order == 4){
				return;
			}else{
				System.out.println("Please input a number between 1 to 4");
			}
		}
	}
	
	public static int getIntFromInput(Scanner input){
		try{
			int order = Integer.valueOf(input.next());
			return order;
		}catch(NumberFormatException e){
			System.out.println("please input number");
		}
		return -1;
	}
	
	public static String getStringFromInput(Scanner input){
		while(true){
			String str = input.nextLine();
			if(str.length() == 0){
				continue;
			}
			return str;
		}
	}
}
